package com.swq.java.code.chapter19.code11;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonCounter extends JFrame {
    private JButton button = new JButton("1");

    public ButtonCounter(String title) {
        super(title);
        //为JButton注册ActionEvent的监听器
        button.addActionListener(new ActionListener() {  //定义一个内部类
                                     public void actionPerformed(ActionEvent evt) {
                                         int count = Integer.parseInt(button.getText());
                                         button.setText(new Integer(++count).toString());  //把JButton的标号加1
                                     }
                                 }
        );

        add(button);
        setSize(100, 100);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String args[]) {
        new ButtonCounter("Hello");
    }
}

